﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using MySql.Data.MySqlClient;


namespace student_systemApp
{
    public partial class AddClassForm : Form
    {
        public AddClassForm()
        {
            InitializeComponent();

        }
        private void button_subbmit_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["UserManagementConnectionString"].ConnectionString;
            string sql = "insert into class(class_name,grade_id,remark)" +
                " values(@className,@gradeId,@remark);";
            using (var connection = new MySqlConnection(connectionString))
            {
                try
                {
                    if (textBox_className.Text.Trim() == "" || textBox_className == null)
                    {
                        MessageBox.Show("班级名称不能为空");
                        return;
                    }
                    if (comboBox_grade.SelectedIndex == 0)
                    {
                        MessageBox.Show("请选择班级年级");
                        return;
                    }
                    connection.Open();
                    string sql1 = "select * from class where class_name = @className" +
                        " and grade_id = @gradeId";
                    MySqlCommand command1 = new MySqlCommand(sql1, connection);
                    command1.Parameters.AddWithValue("@className", textBox_className.Text);
                    command1.Parameters.AddWithValue("@gradeId", comboBox_grade.SelectedValue);
                    if (command1.ExecuteScalar() != null)
                    {
                        MessageBox.Show("班级名称重复，请重新输入");
                        return;
                    }
                    MySqlCommand command = new MySqlCommand(sql, connection);
                    command.Parameters.AddWithValue("@className", textBox_className.Text);
                    command.Parameters.AddWithValue("@gradeId", comboBox_grade.SelectedValue);
                    command.Parameters.AddWithValue("@remark", textBox_remark.Text);

                    int result = command.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("添加成功");

                        // 清空表单内容
                        textBox_className.Clear();
                        comboBox_grade.SelectedIndex = 0;
                        textBox_remark.Clear();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加失败：" + ex.Message);
                }
            }
        }


        private void panel_addClass_Paint(object sender, PaintEventArgs e)
        {
            //ListClassForm listClassForm = new ListClassForm();
            //listClassForm.initGrades();
        }


        //加载年级下拉框
        private void initGrades()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["UserManagementConnectionString"].ConnectionString;
            string sql = "SELECT grade_id,grade_name FROM grade;";

            using (var connection = new MySqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    MySqlCommand command = new MySqlCommand(sql, connection);
                    MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);

                    DataRow row = dt.NewRow();
                    row["grade_name"] = "请选择";
                    row["grade_id"] = 0;
                    dt.Rows.InsertAt(row, 0);

                    //绑定数据
                    comboBox_grade.DataSource = dt;
                    comboBox_grade.DisplayMember = "grade_name";
                    comboBox_grade.ValueMember = "grade_id";

                    //默认选择0
                    comboBox_grade.SelectedIndex = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("查找失败" + ex.Message, "提示");
                }
                connection.Close();
            }
        }

        private void comboBox_grade_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void AddClassForm_Load(object sender, EventArgs e)
        {
            //加载年级下拉框
            initGrades();
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

}


